package cn.iocoder.an.module.business.controller.admin.warningconfig;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.warningconfig.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.warningconfig.WarningConfigDO;
import cn.iocoder.an.module.business.service.warningconfig.WarningConfigService;

@Tag(name = "管理后台 - 预警配置")
@RestController
@RequestMapping("/business/warning-config")
@Validated
public class WarningConfigController {

    @Resource
    private WarningConfigService warningConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建预警配置")
    public CommonResult<Long> createWarningConfig(@Valid @RequestBody WarningConfigSaveReqVO createReqVO) {
        return success(warningConfigService.createWarningConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预警配置")
    public CommonResult<Boolean> updateWarningConfig(@Valid @RequestBody WarningConfigSaveReqVO updateReqVO) {
        warningConfigService.updateWarningConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预警配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteWarningConfig(@RequestParam("id") Long id) {
        warningConfigService.deleteWarningConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除预警配置")
    public CommonResult<Boolean> deleteWarningConfigList(@RequestParam("ids") List<Long> ids) {
        warningConfigService.deleteWarningConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预警配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TransMethodResult
    public CommonResult<WarningConfigRespVO> getWarningConfig(@RequestParam("id") Long id) {
        WarningConfigDO warningConfig = warningConfigService.getWarningConfig(id);
        return success(BeanUtils.toBean(warningConfig, WarningConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预警配置分页")
    @TransMethodResult
    public CommonResult<PageResult<WarningConfigRespVO>> getWarningConfigPage(@Valid WarningConfigPageReqVO pageReqVO) {
        PageResult<WarningConfigDO> pageResult = warningConfigService.getWarningConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarningConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预警配置 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarningConfigExcel(@Valid WarningConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarningConfigDO> list = warningConfigService.getWarningConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "预警配置.xls", "数据", WarningConfigRespVO.class,
                        BeanUtils.toBean(list, WarningConfigRespVO.class));
    }

}